import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

// 布局组件
const Layout = () => import('@/layout/index.vue')

// 路由配置
const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: {
      title: '首页',
      requiresAuth: true
    },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '仪表盘',
          icon: 'HomeFilled',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/customers',
    component: Layout,
    meta: {
      title: '客户管理',
      requiresAuth: true
    },
    children: [
      {
        path: '/customers',
        name: 'CustomerList',
        component: () => import('@/views/customers/index.vue'),
        meta: {
          title: '客户列表',
          icon: 'User',
          requiresAuth: true
        }
      },
      {
        path: '/customers/create',
        name: 'CustomerCreate',
        component: () => import('@/views/customers/CreateCustomer.vue'),
        meta: {
          title: '新增客户',
          requiresAuth: true,
          hidden: true
        }
      },
      {
        path: '/customers/:id',
        name: 'CustomerDetail',
        component: () => import('@/views/customers/CustomerDetail.vue'),
        meta: {
          title: '客户详情',
          requiresAuth: true,
          hidden: true
        }
      }
    ]
  },
  {
    path: '/activities',
    component: Layout,
    meta: {
      title: '活动管理',
      requiresAuth: true
    },
    children: [
      {
        path: '/activities',
        name: 'ActivityList',
        component: () => import('@/views/activities/index.vue'),
        meta: {
          title: '活动列表',
          icon: 'Calendar',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/products',
    component: Layout,
    meta: {
      title: '产品管理',
      requiresAuth: true
    },
    children: [
      {
        path: '/products',
        name: 'ProductList',
        component: () => import('@/views/products/index.vue'),
        meta: {
          title: '产品列表',
          icon: 'Box',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/opportunities',
    component: Layout,
    meta: {
      title: '销售机会',
      requiresAuth: true
    },
    children: [
      {
        path: '/opportunities',
        name: 'OpportunityList',
        component: () => import('@/views/opportunities/index.vue'),
        meta: {
          title: '销售机会',
          icon: 'TrendCharts',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/tours',
    component: Layout,
    meta: {
      title: '出团管理',
      requiresAuth: true
    },
    children: [
      {
        path: '/tours',
        name: 'TourList',
        component: () => import('@/views/tours/index.vue'),
        meta: {
          title: '出团列表',
          icon: 'MapLocation',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/aftercare',
    component: Layout,
    meta: {
      title: '售后服务',
      requiresAuth: true
    },
    children: [
      {
        path: '/aftercare',
        name: 'AftercareList',
        component: () => import('@/views/aftercare/index.vue'),
        meta: {
          title: '售后服务',
          icon: 'Service',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/statistics',
    component: Layout,
    meta: {
      title: '统计分析',
      requiresAuth: true
    },
    children: [
      {
        path: '/statistics',
        name: 'Statistics',
        component: () => import('@/views/statistics/index.vue'),
        meta: {
          title: '统计报表',
          icon: 'DataAnalysis',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    meta: {
      title: '系统管理',
      requiresAuth: true
    },
    children: [
      {
        path: '/system/users',
        name: 'UserManagement',
        component: () => import('@/views/system/Users.vue'),
        meta: {
          title: '用户管理',
          icon: 'UserFilled',
          requiresAuth: true
        }
      },
      {
        path: '/system/settings',
        name: 'SystemSettings',
        component: () => import('@/views/system/Settings.vue'),
        meta: {
          title: '系统设置',
          icon: 'Setting',
          requiresAuth: true
        }
      },
      {
        path: '/system/data-maintenance',
        name: 'DataMaintenance',
        component: () => import('@/views/system/DataMaintenance.vue'),
        meta: {
          title: '数据维护',
          icon: 'Database',
          requiresAuth: true
        }
      },
      {
        path: '/system/monitoring',
        name: 'SystemMonitoring',
        component: () => import('@/views/system/SystemMonitoring.vue'),
        meta: {
          title: '系统监控',
          icon: 'Monitor',
          requiresAuth: true
        }
      },
      {
        path: '/system/integration',
        name: 'SystemIntegration',
        component: () => import('@/views/system/SystemIntegration.vue'),
        meta: {
          title: '系统集成',
          icon: 'Connection',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面不存在',
      requiresAuth: false
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - CRM旅游管理系统`
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next('/login')
  } else if (to.path === '/login' && authStore.isAuthenticated) {
    next('/dashboard')
  } else {
    next()
  }
})

export default router